# 数据归一化，独热
import numpy as np


def train_data():
    data_x = []
    data_y = []
    with open('dataset/train.csv') as f:
        for line in f.readlines():
            line_data = line.strip().split(',')
            data_x.append(line_data[1:])
            data_y.append(line_data[0])
    max_bm = []
    for _ in range(26):
        max_bm.append(0)
    for i in data_x:
        for j in range(13, 39):
            i[j] = int(i[j])
            if i[j] > max_bm[j-13]:
                max_bm[j-13] = i[j]
    bm_sum = 0
    for i in range(26):
        bm_sum += max_bm[i]
    sum_x = []
    for _ in range(13):
        sum_x.append(1)
    for i in data_x:
        for j in range(13):
            i[j] = float(i[j])
            sum_x[j] += i[j]
    for i in range(13):
        sum_x[i] /= len(data_x)
    for i in data_x:
        for j in range(13):
            i[j] /= sum_x[j]
    train_x = []
    for i in data_x:
        temp = np.zeros((1, 793))
        temp = temp.reshape(-1)
        for j in range(13):
            temp[j] = i[j]
        for j in range(26):
            bi = bin(int(i[j+13]))
            for k in range(len(bi)-2):
                temp[k + j * 30 + 13] = bi[-(k+1)]
        train_x.append(temp)
    return train_x, data_y


def test_data():
    data_x = []
    t = 0
    with open('dataset/test.csv') as f:
        for line in f.readlines():
            t += 1
            line_data = line.strip().split(',')
            data_x.append(line_data)
            if t == 1000:
                break
    max_bm = []
    for _ in range(26):
        max_bm.append(0)
    for i in data_x:
        for j in range(13, 39):
            i[j] = int(i[j])
            if i[j] > max_bm[j - 13]:
                max_bm[j - 13] = i[j]
    bm_sum = 0
    for i in range(26):
        bm_sum += max_bm[i]
    sum_x = []
    for _ in range(13):
        sum_x.append(1)
    for i in data_x:
        for j in range(13):
            i[j] = float(i[j])
            sum_x[j] += i[j]
    for i in range(13):
        sum_x[i] /= len(data_x)
    for i in data_x:
        for j in range(13):
            i[j] /= sum_x[j]
    test_x = []
    for i in data_x:
        temp = np.zeros((1, 793))
        temp = temp.reshape(-1)
        for j in range(13):
            temp[j] = i[j]
        for j in range(26):
            bi = bin(int(i[j + 13]))
            for k in range(len(bi) - 2):
                temp[k + j * 30 + 13] = bi[-(k + 1)]
        test_x.append(temp)
    return test_x